package com.qbw.app.module.common.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.View;

import com.qbw.app.R;
import com.qbw.app.databinding.CommonFragmentUpdateBinding;
import com.qbw.app.model.Version;
import com.qbw.core.base.BaseDialogFragment;
import com.qbw.l.L;

/**
 * @author QBW
 * @date 2019/10/16
 */
public class UpdateDialogFragment extends BaseDialogFragment<CommonFragmentUpdateBinding> implements View.OnClickListener {

    private static final String KEY_VERSION = "key_version";

    private Version mVersion;

    public static UpdateDialogFragment createInstance(Version version) {
        UpdateDialogFragment ins = new UpdateDialogFragment();
        Bundle args = new Bundle();
        args.putSerializable(KEY_VERSION, version);
        ins.setArguments(args);
        return ins;
    }

    @Override
    protected int onGetLayoutId() {
        return R.layout.common_fragment_update;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getArguments() : savedInstanceState;
        mVersion = (Version) args.getSerializable(KEY_VERSION);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding.setVersion(mVersion);
        mBinding.tvSure.setOnClickListener(this);
        mBinding.tvCancel.setOnClickListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_sure) {
            if (mVersion.getIsUpdate() != Version.FORCE_UPDATE) {
                dismiss();
            }
            if (mVersion.hasUpdate()) {
                /*XDownloader.getInstance()
                           .addTask(XDownloader.Task.createInstance(mVersion.getUpdateUrl(),
                                                                    mVersion.getLatestVersion() + ".apk")
                                                    .setType(Constant.D_TYPE_UPDATE));*/
                Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                                                  Uri.parse(mVersion.getUpdateUrl()));
                startActivity(browserIntent);
            } else {
                L.GL.e("app's new version url is empty!!!");
            }
        } else if (vid == R.id.tv_cancel) {
            dismiss();
            if (mVersion.getIsUpdate() == Version.FORCE_UPDATE) {
                getActivity().finishAffinity();
            }
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(KEY_VERSION, mVersion);
    }
}
